package cn.stephen.study.demoproject.task;


import cn.stephen.study.demoproject.dao.dbone.ReportMapper;
import cn.stephen.study.demoproject.dao.dbtwo.StationEquipmentMapper;
import cn.stephen.study.demoproject.util.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@EnableScheduling
public class ReportTask {

    @Autowired
    private StationEquipmentMapper stationEquipmentMapper;
    @Autowired
    private ReportMapper reportMapper;

    private static ReportTask reportTask;
    private static WordUtil wordUtil;

    @PostConstruct
    public void init() {
        reportTask = this;
        reportTask.stationEquipmentMapper = this.stationEquipmentMapper;
        wordUtil = new WordUtil();
        reportTask.reportMapper = this.reportMapper;
    }

    /**
     * 每周一 零点开始统计上周 周报表
     */

//    @Scheduled(cron = "* 0/2 * * * ? ")//每两分钟
    @Scheduled(cron = "0 0 0 ? * 1")
    public void reportWeeKTask() {
        //获取所有站点
//        List<Map<String, String>> allStations = reportTask.stationEquipmentMapper.findAllStations();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, String>> allStations  =new ArrayList<>();
        Map map=new HashMap();
        map.put("STATIONCODE","42098100200104000001");
        map.put("STATIONNAME","云应膏盐Ⅰ号监测网");
        allStations.add(map);
        Map map1=new HashMap();
        map1.put("STATIONCODE","42098100200604000000");
        map1.put("STATIONNAME","云应膏盐Ⅱ号监测网");
        allStations.add(map1);
        Map map2=new HashMap();
        map2.put("STATIONCODE","42092310700904000000");
        map2.put("STATIONNAME","云应膏盐Ⅲ号监测网");
        allStations.add(map2);
        Map map3=new HashMap();
        map3.put("STATIONCODE","42098110800204000000");
        map3.put("STATIONNAME","云应膏盐Ⅴ号监测网");
        allStations.add(map3);
        Map map4=new HashMap();
        map4.put("STATIONCODE","42098100500104000000");
        map4.put("STATIONNAME","云应膏盐Ⅳ号监测网");
        allStations.add(map4);


        String dateStr = sdf.format(new Date());
        for (Map<String, String> allStation : allStations) {
            String fileName = allStation.get("STATIONNAME")+"-"+"周报表" + dateStr;
            MyRunnable myRunnable = new MyRunnable();
            myRunnable.setStationCode(allStation.get("STATIONCODE"));
            myRunnable.setType(1);
            myRunnable.setFileName(fileName);
            Thread thread = new Thread(myRunnable);
            thread.start();
        }

    }

    class MyRunnable implements Runnable {
        private String stationCode;
        private Integer type;
        private String fileName;

        public void setStationCode(String stationCode) {
            this.stationCode = stationCode;
        }

        public void setType(Integer type) {
            this.type = type;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        @Override
        public void run() {
            wordUtil.createDoc(stationCode, type, fileName);
        }
    }

    /**
     * 每月一号零点开始统计上月报表
     */
    @Scheduled(cron = "0 0 0 1 1/1 ? ")
    public void reportWeeKTask1() {
        //获取所有站点
//        List<Map<String, String>> allStations = reportTask.stationEquipmentMapper.findAllStations();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Map<String, String>> allStations  =new ArrayList<>();
        Map map=new HashMap();
        map.put("STATIONCODE","42098100200104000001");
        map.put("STATIONNAME","云应膏盐Ⅰ号监测网");
        allStations.add(map);
        Map map1=new HashMap();
        map1.put("STATIONCODE","42098100200604000000");
        map1.put("STATIONNAME","云应膏盐Ⅱ号监测网");
        allStations.add(map1);
        Map map2=new HashMap();
        map2.put("STATIONCODE","42092310700904000000");
        map2.put("STATIONNAME","云应膏盐Ⅲ号监测网");
        allStations.add(map2);
        Map map3=new HashMap();
        map3.put("STATIONCODE","42098110800204000000");
        map3.put("STATIONNAME","云应膏盐Ⅴ号监测网");
        allStations.add(map3);
        Map map4=new HashMap();
        map4.put("STATIONCODE","42098100500104000000");
        map4.put("STATIONNAME","云应膏盐Ⅳ号监测网");
        allStations.add(map4);
        String dateStr = sdf.format(new Date());
        for (Map<String, String> allStation : allStations) {
            String fileName =allStation.get("STATIONNAME") +"-"+ "月报表" +  dateStr;
            MyRunnable myRunnable = new MyRunnable();
            myRunnable.setStationCode(allStation.get("STATIONCODE"));
            myRunnable.setType(2);
            myRunnable.setFileName(fileName);
            Thread thread = new Thread(myRunnable);
            thread.start();
        }
    }
}

